﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using GC_RF.Domain.Abstractions.Interfaces.Factories;
using GC_RF.Domain.Abstractions.Interfaces.Helpers;
using GC_RF.Domain.Abstractions.Interfaces.Services;
using GC_RF.Domain.Constants;
using GC_RF.Domain.Enums;
using GC_RF.Site.Abstractions;
using GC_RF.Site.Attributes;
using GC_RF.Site.Extensions.Models.News;
using GC_RF.Site.Models;
using GC_RF.Site.Models.News;
using GC_RF.Site.Resources;
using GC_RF.Site.Resources.Constants;

namespace GC_RF.Site.Controllers
{
	public class NewsController : BaseController
	{
		#region Variables

		private readonly INewsService _newsService;
		private readonly IHttpPostedFileManager _httpPostedFileManager;

		#endregion

		#region Constructors

		public NewsController(IServiceFactory serviceFactory, IHelperFactory helperFactory)
			: base(serviceFactory)
		{
			_newsService = ServiceFactory.CreateNewsService();
			if (_newsService == null)
				throw new ArgumentNullException(string.Format("newsService"));

			if (helperFactory == null)
				throw new ArgumentNullException(string.Format("helperFactory"));

			_httpPostedFileManager = helperFactory.CreateHttpPostedFileManager();
			if (_httpPostedFileManager == null)
				throw new ArgumentNullException(string.Format("httpPostedFileManager"));
		}

		#endregion

		#region Actions

		[AjaxOrChildActionOnly]
		public ActionResult News(int? pageNumber)
		{
			var model = new GridModel<NewsModel>();
			FillNewsGridModel(model, pageNumber != null ? pageNumber.Value - 1 : 0);
			return View(model);
		}

		[UserAuthorize(UserRoles = UserRole.Administrator | UserRole.Moderator)]
		[HttpGet]
		public ActionResult Edit(int? id)
		{
			var model = new NewsModel();
			return View(model);
		}

		[UserAuthorize(UserRoles = UserRole.Administrator | UserRole.Moderator)]
		[HttpPost, ValidateInput(false)]
		public ActionResult Edit(NewsModel model)
		{
			if (IsValid && model != null && Execute(() => _newsService.Save(model.ToNewsData())))
				return RedirectToAction<HomeController>(o => o.Index());
			return View(model);
		}

		public string UploadFile()
		{
			var file = Request.Files[Keys.RequestFiledata];
			var result = string.Empty;
			Execute(() =>
			{
				result = _httpPostedFileManager.SaveAs(file, Server.MapPath(AppConfig.NewsImagesFolderPath));
				if (!string.IsNullOrWhiteSpace(result))
					result = StaticResources.GetNewsImageUrl(result);
			});
			return result;
		}

		#endregion

		#region Internal Implementation

		private void FillNewsGridModel(GridModel<NewsModel> model, int pageNumber)
		{
			if (model == null) return;

			var news = Execute(() => _newsService.GetNews(pageNumber, UIHelper.NewsPageSize));

			if (news != null)
				model.Items = new List<NewsModel>(news.Select(n => new NewsModel(n)));

			model.Pager.PageSize = UIHelper.NewsPageSize;
			model.Pager.PageNumber = pageNumber + 1;
			model.Pager.Count = Execute(() => _newsService.NewsCount());
		}

		#endregion
	}
}